package com.xunsiya.interceptor;

import com.xunsiya.common.config.UploadConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.MultipartConfigElement;

/**
 * 注册拦截器
 *
 * @author SiChunyang
 * @version 1.0
 *          <p>
 *          <br/>
 *          <br/>修订人		修订时间			描述信息
 *          <br/>-----------------------------------------------------
 *          <br/>SiChunyang		2018/5/9		初始创建
 */
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Autowired
    private SysInterceptor sysInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //拦截规则：除了login，其他都拦截判断
        registry.addInterceptor(sysInterceptor)
                .addPathPatterns("/**");
    }

    @Autowired
    private UploadConfig uploadConfig;

    /**
     * 限定上传文件最大为200MB
     * @return MultipartConfigElement
     */
    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        //单个文件最大
        factory.setMaxFileSize(uploadConfig.getMaxFileSize());
        //设置总上传数据总大小
        factory.setMaxRequestSize(uploadConfig.getMaxRequestSize());
        return factory.createMultipartConfig();
    }
}
